In this example we add our own controls to Play / Pause Video.
ContentView.swift
import SwiftUI
import AVKit
struct ContentView: View {
var player = AVPlayer(url: Bundle.main.url(forResource: "Pexels", withExtension: "mp4")!)
var body: some View {
VStack {
PlayerView(player: player)
Button("Play" ) { self.player.play () }
Button("Pause") { self.player.pause() }
}
}
}
In this example we will change the code a bit to make better distinction between Controller and Player.
This will also allow us to have references to both of them.
Wrapper inside ControllerView.swift will be simplified so that all of the code is in ContentView.swift.
ControllerView.swift
import SwiftUI
import AVKit
struct ControllerView: UIViewControllerRepresentable {
let controller : AVPlayerViewController
func makeUIViewController(context: UIViewControllerRepresentableContext<ControllerView>) ->
AVPlayerViewController {
return controller
}
public func updateUIViewController(_ uiViewController: AVPlayerViewController, context:
UIViewControllerRepresentableContext<ControllerView>) {}
}
ContentView.swift
import SwiftUI
import AVKit
struct ContentView: View {
var player = AVPlayer(url: Bundle.main.url(forResource: "Pexels", withExtension: "mp4")!)
var controller = AVPlayerViewController()
init() { controller.player = player }
var body: some View {
ControllerView(controller: controller)
}
}